<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>发表动弹frm</title>
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
    <style type="text/css">
		body {
			background-color: #efeff4;
		}
		.aui-content {
			margin-bottom: 0px;
		}
		textarea {
			border: 0px;
			margin-bottom: 5px;
			border-radius: 0;
		}
		.aui-user-view-cell {
			padding: 5px 15px;
		}
		.aui-user-view-cell:first-child:after {
			border-top: 0;
		}
		em {
			margin-top: 5px;
		}
    </style>
</head>
<body>
    <div class="aui-content">
    	<textarea rows="5" placeholder="今天你动弹了吗（最多不超过200字）" id="content"></textarea>
    </div>
    
    <div class="aui-content">
        <ul>
            <li class="aui-list-view">
                <div class="aui-user-view-cell aui-img">
                    <div class="aui-img-body">
                        <span class="aui-text-default">上传照片<em class="aui-pull-right aui-iconfont aui-icon-unfold"></em></span>
                    </div>
                </div>
                <ul class="aui-list-view aui-grid-view" id="photoList">
                    <li id="addImgBtn" class="aui-list-view-cell aui-img aui-col-xs-4">
                        <img class="aui-img-object" src="../image/addImgBtn.png" tapmode onclick="selectImage()">
                    </li>
                    <li id="delImgBtn" class="aui-list-view-cell aui-img aui-col-xs-4" style="display: none;">
                        <img class="aui-img-object" src="../image/delImgBtn.png" tapmode onclick="delImage()">
                    </li>
                </ul>
            </li>
        </ul>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/zepto.min.js"></script>
<script type="text/javascript" src="../script/OSChinaApi.js" ></script>
<script type="text/javascript" src="../script/common.js" ></script>
<script type="text/javascript">
    apiready = function () {
		$('#content').focus();
    }
    
    // 选择照片
	function selectImage(){
		$('#content').blur();
	    api.actionSheet({
	        title: '请选择图片来源',
	        cancelTitle: '取消',
	        buttons: ['拍照','从相册选取']
	    },function(ret,err){
	        var winWidth = api.winWidth;
	        var listHeight = winWidth/3;
	        var sourceType = '';
	        if(ret.buttonIndex==1){
	        	sourceType = 'camera'
	        }else if(ret.buttonIndex==2){
	        	sourceType = 'library'
	        }else{
	        	return false;
	        }
	        
            api.getPicture({
                sourceType: sourceType,
                encodingType: 'jpg',
                mediaValue: 'pic',
                destinationType: 'url',
                allowEdit: false,
                quality: 90,
                saveToPhotoAlbum: false
            }, function(ret, err){ 
                if (ret.data) {
					$api.prepend($api.byId("photoList"), '<li id="photo" class="aui-list-view-cell aui-img aui-col-xs-4" style="height:'+listHeight+'px;"><img id="photoPath" class="aui-img-object" src="'+ret.data+'" tapmode onclick="imageBrowser(\''+ret.data+'\')"></li>');
					$('#addImgBtn').hide();
					$('#delImgBtn').show();
					api.parseTapmode();
                }
            });
	    });
	}
	
	// 删除照片
	function delImage(){
		$('#content').blur();
		var photo = $api.byId("photo");
		if(photo){
			$api.remove(photo);
		}
		
		$('#delImgBtn').hide();
		$('#addImgBtn').show();
	}
	
	// 发表动弹
	function pub(){
		$('#content').blur();
		
		var uid = intval($api.getStorage('user_uid'));
		if( uid<1 ){
			api.toast({
			    msg: '请先登录',
			    duration:2000,
			    location: 'middle'
			});
			return false;
		}
		var photoPath = '';
		var content = $api.val($api.byId("content"));
		var photoPath_el = $api.byId("photoPath");
		if(photoPath_el){
			photoPath = $api.attr($api.byId("photoPath"), 'src');
		}
		
		if( content.length<1 ){
			api.toast({
			    msg: '请填写动弹内容',
			    duration:2000,
			    location: 'middle'
			});
			$('#content').focus();
			return false;
		}
		
		api.showProgress({
		    style: 'default',
		    animationType: 'fade',
		    title: '发表中...',
		    text: '',
		    modal: true
		});
		
		var url = OpenAPI.tweet_pub;
		api.ajax({
		    url: url,
		    method: 'post',
		    timeout: 30,
		    dataType: 'text',
		    returnAll:false,
		    data:{
		        values: {
		        			msg: content,
		        			teamid: 0,
		        			uid: uid,
		        		},
		        files: {img: photoPath}
		    }
		},function(ret,err){
			api.hideProgress();
		    if (ret) {
				var trans = api.require('trans');
				trans.parse({
				    data:ret
				},function(ret,err){
				    if(ret) {
						if(typeof(ret.oschina.result.errorCode) != 'undefined' && parseInt(ret.oschina.result.errorCode)==1){
							api.toast({
							    msg: '动弹发表成功',
							    duration:2000,
							    location: 'middle'
							});
							
							api.execScript({
							    name: 'root',
							    frameName: 'tweet_list',
							    script: 'scroll(); api.refreshHeaderLoading();'
							});
							
							closeWin();
						}else if( typeof(ret.oschina.result.errorMessage) != 'undefined' ){
							api.toast({
							    msg: ret.oschina.result.errorMessage,
							    duration:2000,
							    location: 'middle'
							});
						}
				    }else{
				        log(err.msg);
				    }
				});
		    }else {
		        log('错误码：'+err.code+'；错误信息：'+err.msg+'网络状态码：'+err.statusCode);
		    	showAjaxErrMsg(err.code);
		    }
		});
	}
	
	// 关闭当前窗口
    function closeWin(){
		api.closeWin({
		    animation: {
		        type: 'reveal',
		        subType: 'from_top',
		    }
		});
    }
</script>
</html>